from HopPath import HopPath
from APIConnection import APIConnection
from CustomTask import CustomTask
import threading

class PathHandler:
    IDs = []
    response = []
    Hop1 = []
    Hop2 = []
    Hop3 = []
    def __init__(self,id1,id2):
        self.IDs.append({'Id':id1})
        self.IDs.append({'Id':id2})
        self.IDs[0]['type'] = self.CheckId(self.IDs[0].get('Id'))
        self.IDs[1]['type'] = self.CheckId(self.IDs[1].get('Id'))
    
    def CheckId(self,ID):
        a = APIConnection()
        data = a.GetEntityById(ID)
        # 0 meant Id, 1 meant AuId
        if data.has_key('AA'):
            return 0
        else:
            return 1

    def ChoosePathMethod(self):
        path = HopPath()
        if self.IDs[0]['type']==0 and self.IDs[1]['type']==0:
            tc=[]
            ct=[]
            ct.append(CustomTask(path.Id2Id_RId_Loop))
            ct.append(CustomTask(path.Id2Id_CId_Loop))
            ct.append(CustomTask(path.Id2Id_FId_Loop))
            ct.append(CustomTask(path.Id2Id_JId_Loop))
            ct.append(CustomTask(path.Id2ID_AuId_Loop))
            for i in ct:
                tc.append(threading.Thread(target=i.run,args=(self.IDs[0].get('Id'),self.IDs[1].get('Id'))))
            for t in tc:
                t.start()
            for t in tc:
                t.join()
            self.Hop1=ct[0].get_result()
            self.Hop2 = ct[1].get_result()
            self.Hop2.extend(ct[2].get_result())
            self.Hop2.extend(ct[3].get_result())
            self.Hop2.extend(ct[4].get_result())
            #self.Hop1 = path.Id2Id_RId_Loop(self.IDs[0].get('Id'),self.IDs[1].get('Id'))
            #self.Hop2 = path.Id2Id_CId_Loop(self.IDs[0].get('Id'),self.IDs[1].get('Id'))
            #self.Hop2.extend(path.Id2Id_FId_Loop(self.IDs[0].get('Id'),self.IDs[1].get('Id')))
            #self.Hop2.extend(path.Id2Id_JId_Loop(self.IDs[0].get('Id'),self.IDs[1].get('Id')))
            #self.Hop2.extend(path.Id2ID_AuId_Loop(self.IDs[0].get('Id'),self.IDs[1].get('Id')))
        elif self.IDs[0]['type']==0 and self.IDs[1]['type']==1:
            #self.Hop1 = path.Id2AuId_Id_Loop(self.IDs[0].get('Id'),self.IDs[1].get('Id'))
            #self.Hop2 = path.Id2AuId_RId(self.IDs[0].get('Id'),self.IDs[1].get('Id'))
            tc=[]
            ct=[]
            Hop1=[]
            Hop2=[]
            ct.append(CustomTask(path.Id2AuId_Id_Loop))
            ct.append(CustomTask(path.Id2AuId_RId))
            for i in ct:
                tc.append(threading.Thread(target=i.run,args=(self.IDs[0].get('Id'),self.IDs[1].get('Id'))))
            for t in tc:
                t.start()
            for t in tc:
                t.join()
            self.Hop1=ct[0].get_result()
            self.Hop2=ct[1].get_result()
        elif self.IDs[0]['type']==1 and self.IDs[1]['type']==0:
            tc=[]
            ct=[]
            Hop1=[]
            Hop2=[]
            ct.append(CustomTask(path.AuId2Id_AuId_Loop))
            ct.append(CustomTask(path.AuId2Id_RId))
            for i in ct:
                tc.append(threading.Thread(target=i.run,args=(self.IDs[0].get('Id'),self.IDs[1].get('Id'))))
            for t in tc:
                t.start()
            for t in tc:
                t.join()
            self.Hop1=ct[0].get_result()
            self.Hop2=ct[1].get_result()
            #self.Hop1 = path.AuId2Id_AuId_Loop(self.IDs[0].get('Id'),self.IDs[1].get('Id'))
            #self.Hop2 = path.AuId2Id_RId(self.IDs[0].get('Id'),self.IDs[1].get('Id'))
        else:
            self.Hop2 = path.AuId2AuId_AfId_Loop(self.IDs[0].get('Id'),self.IDs[1].get('Id'))
        
    def GetResponse(self):
        self.response = []
        self.ChoosePathMethod()
        self.response.append(self.Hop1)
        if len(self.Hop2)>0:
            for i in self.Hop2:
                self.response.append(i)
        if len(self.Hop3)>0:
            for i in self.Hop3:
                self.response.append(i)
        return self.response

if __name__ == "__main__":
    for i in range(3):
        handler = PathHandler(2115688000,2118306842)
        print(handler.IDs)
        response = handler.GetResponse()
        print(handler.Hop1)
        print(handler.Hop2)
        print(handler.Hop3)
        print(response)
